package cn.com.homeyoda.aimate.aihealth.ui.health;

import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.RequiresApi;
import androidx.databinding.library.baseAdapters.BR;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import cn.com.homeyoda.aimate.aihealth.R;
import cn.com.homeyoda.aimate.aihealth.app.AppViewModelFactory;
import cn.com.homeyoda.aimate.aihealth.base.BaseActivity;
import cn.com.homeyoda.aimate.aihealth.bean.SportTitleBean;

import cn.com.homeyoda.aimate.aihealth.databinding.ActivitySportBinding;
import cn.com.homeyoda.aimate.aihealth.ui.health.adapter.SportTitleAdapter;
import cn.com.homeyoda.aimate.aihealth.ui.health.viewModel.HealthModel;

import java.util.ArrayList;
import java.util.List;

//完善信息
public class SportActivity extends BaseActivity<ActivitySportBinding, HealthModel>{

    private SportTitleAdapter sportTitleAdapter;
    private List<SportTitleBean> sportTitleBeanList = new ArrayList<>();

    private SportTitleBean sportTitleBean = new SportTitleBean();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public HealthModel initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(HealthModel.class);
    }

    @Override
    public void initData() {
        super.initData();
        viewModel.sportTitleData();
        initView();
        initRecyclerView();
    }

    private void initView() {


        binding.back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });


    }


    private void initRecyclerView() {
        sportTitleAdapter = new SportTitleAdapter(R.layout.sport_title_item, sportTitleBeanList, getApplicationContext());
        binding.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false));
        binding.setAdapter(sportTitleAdapter);
        sportTitleAdapter.setPos(viewModel.getDatePos());


        sportTitleAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                sportTitleBean = sportTitleBeanList.get(position);
                Log.i("haha",sportTitleBean.getWeek());
                Log.i("haha",sportTitleBean.getDate());
                sportTitleAdapter.setPos(position);
            }
        });

    }



    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onDestroy() {
        super.onDestroy();

    }



    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.stateLiveData.observe(this,sportTitleBeans -> {
            sportTitleBeanList = sportTitleBeans;
            sportTitleAdapter.setNewData(sportTitleBeanList);
        });
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_sport;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }
}
